//app.js
const app = getApp();
var sysConfig = require('config.js');
App({
  onLaunch: function() {
    // 获取系统状态栏信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
         	this.globalData.Custom = capsule;
        	this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
        	this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
  },



  login:function(){
    var _this = this;
    // 根据openid默认登录
    return new Promise(function (resolve, reject) {
      if(_this.globalData.openId == null){
        wx.login({
          success (res) {
            if (res.code) {
              wx.request({
                url: sysConfig.baseurl+'site/openid',
                data: {
                  code: res.code,
                  name:'CHENGzhang921',
                  password:'wadFwSrsr'
                  // api_token :_this.globalData.token,
                },
                success: function(res){
                  res = res.data;
                  _this.globalData.openId = res.data.openid;
                  _this.globalData.token = res.data.token;
                  _this.globalData.secret = res.data.secret;
                  console.log('_this.globalData.openId')
                  console.log(_this.globalData.openId)
                  if(res.data.parent){
                    _this.globalData.parent = res.data.parent;
                    _this.globalData.login = true;
                  }
                  console.log(_this.globalData);
                  resolve(res);
                },
              })
            } else {
              console.log('登录失败！' + res.mag)
              reject('error');
            }
          }
        })
      }else{
        resolve(res);
      }
    })

  },

  watch:function(method){
    var obj = this.globalData;
    Object.defineProperty(obj,"name", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this.obj = value;
        console.log('是否会被执行2')
        method(value);
      },
      get:function(){
        return obj;
      }
    })
  },

  globalData: {
    login:false,
    parent:{},
    userInfo: {},
    token:null,
    openId: null,
    appid: sysConfig.appid,
    secret: null,
    baseurl: sysConfig.baseurl,
    host: sysConfig.host,
  }
})
